<template>
	<div class="system-edit-dept-container">
		<el-dialog  :close-on-click-modal="false" :title="(acType=='edit' ? '修改' : '添加') + '部门'" v-model="isShowDialog" width="769px">
			<el-form ref="formRef" :model="ruleForm" :rules="rules" size="default" label-width="90px">
				<el-row :gutter="35">

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="部门名称" prop="deptName">
							<el-input v-model="ruleForm.deptName" placeholder="请输入部门名称" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序">
							<el-input-number v-model="ruleForm.orderNum" :min="0" :max="999" controls-position="right"
								placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="部门状态">
							<el-switch v-model="ruleForm.status" :active-value="0" :inactive-value="1" inline-prompt
								active-text="正常" inactive-text="停用"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ acType=='edit' ? '修 改' :
						'添加'}}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent, getCurrentInstance, ref, unref } from 'vue';
import { editDeptApi, addDeptApi } from "@/api/auth/dept";
import { ElMessage } from "element-plus";

// 定义接口来定义对象的类型
interface TableDataRow {
	deptName: string;
	id: number;
	parentId: number;
	children?: TableDataRow[];
}
interface RuleFormState {
	parentId?: number;
	deptName: string;
	orderNum: number;
	status: number;
	label?: string;
	disabled?: boolean;
	id?: number;
	deptId?:number
}
interface DeptSate {
	isShowDialog: boolean;
	ruleForm: RuleFormState;
	deptData: Array<TableDataRow>;
	rules: object;
}

export default defineComponent({
	name: 'systemEditDept',
	props:{
		acType: {
      type: String,
      default: () => 'add'
    }
	},
	setup(prop, { emit }) {
		const { proxy } = getCurrentInstance() as any;
		const formRef = ref<HTMLElement | null>(null);
		const state = reactive<DeptSate>({
			isShowDialog: false,
			ruleForm: {
				parentId: null, // 上级部门
				deptName: '', // 部门名称
				orderNum: 0,
				status: 0,
			},
			deptData: [], // 部门数据
			rules: {
				deptName: [
					{ required: true, message: "部门名称不能为空", trigger: "blur" },
				]
			}
		});
		// 打开弹窗
		const openDialog = (row?: number|RuleFormState) => {
			resetForm()
			if (row && typeof row === "object") {
				const {id,label,disabled,orderNum} =row
				state.ruleForm = {
					orderNum,
					deptId:id,
					deptName:label,
					status:disabled?0:1,
				}
			} else if (row && typeof row === 'number') {
				state.ruleForm.parentId = row
			}
			
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			const formWrap = unref(formRef) as any;
			if (!formWrap) return;
			formWrap.validate((valid: boolean) => {
				if (valid) {
					if (prop.acType=='add') {
						//添加
						addDeptApi(state.ruleForm).then(() => {
							ElMessage.success('角色添加成功');
							closeDialog(); // 关闭弹窗
							emit('deptList')
						});
					} else {
						//修改
						editDeptApi(state.ruleForm).then(() => {
							ElMessage.success('角色修改成功');
							closeDialog(); // 关闭弹窗
							emit('deptList')
						});
					}
				}
			});
		};
		const resetForm = () => {
			state.ruleForm = {
				parentId: null, // 上级部门
				deptName: '', // 部门名称
				orderNum: 0,
				status: 0,
			}
		};
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			formRef,
			...toRefs(state),
		};
	},
});
</script>
